package com.tosit.driverschool.service;

import com.tosit.driverschool.common.entity.Subject;
import com.tosit.driverschool.dao.SubjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    SubjectRepository subjectRepository;

    @Override
    public List<Subject> findAll() {
        List<Subject> all = subjectRepository.findAll();

        String ans = "";
        String opt = "";
        for (Subject s:all){
            //处理单选题
            if (s.getType() == 0){
                opt = "A." + s.getOptiona() + " B." + s.getOptionb() + " C." + s.getOptionc() + " D." + s.getOptiond();
                s.setOptions(opt);
                switch(s.getAnswer()){
                    case 1:
                        ans = "A";
                        break;
                    case 2:
                        ans = "B";
                        break;
                    case 4:
                        ans = "C";
                        break;
                    case 8:
                        ans = "D";
                }
                s.setAns(ans);
            }
            //处理判断题
            else{
                opt = "是 否";
                s.setOptions(opt);
                ans = s.getAnswer()==1?"是":"否";
                s.setAns(ans);

            }
        }
        return all;
    }

    @Override
    public void deleteById(Integer id) {
        subjectRepository.deleteById(id);
    }

    @Override
    public void deleteByIds(List<String> list) {
        List<Integer> l = new ArrayList<>();
        for (String s:list){
            l.add(Integer.parseInt(s));
        }
//        subjectRepository.deleteByIds(l);
    }

    @Override
    public void saveSubject(HttpServletRequest req) {

        //设置题目及选项
        Subject sub = new Subject();
        sub.setProblem(req.getParameter("problem"));
        sub.setOptiona(req.getParameter("optiona"));
        sub.setOptionb(req.getParameter("optionb"));
        sub.setOptionc(req.getParameter("optionc"));
        sub.setOptiond(req.getParameter("optiond"));

        //设置题目类型
        short type = Short.parseShort(req.getParameter("subject_type"));
        sub.setType((byte)type);

        //按照规定(A=1,B=2,C=4,D=8)转换答案为数字
        int answer = 0;
        if(type == 0 || type == 1){
            String[] ss = req.getParameter("answer").split(",");
            for (String s : ss){
                switch(s){
                    case "A":
                        answer += 1;
                        break;
                    case "B":
                        answer += 2;
                        break;
                    case "C":
                        answer += 4;
                        break;
                    case "D":
                        answer += 8;
                }
            }
        }
        else{
            answer = req.getParameter("answer").equals("是")?1:2;
        }
        sub.setAnswer(answer);

        //设置媒体类型
       byte mType =  Byte.parseByte(req.getParameter("media_type"));
//        byte mType = Short.parseShort(req.getParameter("media_type"));
        sub.setMtype(mType);

        //根据媒体类型设置媒体文件路径
        if(mType == 1){
            sub.setMpath("/upload/image/" + req.getParameter("mpath"));
        }
        else{
            sub.setMpath("/upload/video/" + req.getParameter("mpath"));
        }

        //设置读题语音路径
        sub.setMusicpath("/uplaod/music/" + req.getParameter("musicpath"));

        //设置答题技巧
        sub.setExplain(req.getParameter("subject_trick"));

        //设置技巧语音
        sub.setDmusicpath("/upload/music/" + req.getParameter("dmusicpath"));

        //设置ID
        sub.setId(1111);
        sub.setHtmltitle("1");
        sub.setHtmla("1");
        sub.setHtmlb("2");
        sub.setHtmlc("3");
        sub.setHtmld("4");
        sub.setAns("emp");
        sub.setOptions("emp");
        System.out.println(sub.toString());
        subjectRepository.save(sub);


    }
}
